"use strict";

import axios from "axios";
import Message from './Message';

let config = {
	baseURL: global.globalData.api,
};

const _axios = axios.create(config);

_axios.interceptors.request.use(
	function (config) {
		// document.getElementById('loading').style.display = 'block';
		return config;
	},
	function (error) {
		// setTimeout(() => {
		// 	document.getElementById('loading').style.display = 'none';
		// }, 500);
		Message.Message({
			type: 'error',
			message: '网络连接失败，请检查一下网络！'
		});
		return Promise.reject(error);
	}
);

_axios.interceptors.response.use(
	function (response) {
		// setTimeout(() => {
		// 	document.getElementById('loading').style.display = 'none';
		// }, 500);
		if (response.data.code === 0 || response.data.code===undefined) {
			return response.data;
		} else {
			Message.Message({
				type: 'error',
				message: response.data.msg
			});
			return Promise.reject(response.data);
		}
	},
	function (error) {
		Message.Message({
			type: 'error',
			message: '网络连接失败，请检查一下网络！'
		});
		// setTimeout(() => {
		// 	document.getElementById('loading').style.display = 'none';
		// }, 500);
		return Promise.reject(error);
	}
);

export default _axios;
